package com.xiaoyu.tool.messageQueue.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.CustomExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * rabbitmq配置
 * */
@Configuration
@EnableConfigurationProperties(QueueProperties.class)
public class ExpireAndDeleteOrderDelayQueueConfiguration {

    private final QueueProperties properties;

    public ExpireAndDeleteOrderDelayQueueConfiguration(QueueProperties properties) {
        this.properties = properties;
    }

    @Bean(name = "expireAndDeleteOrderDelayQueue")
    Queue expireAndDeleteOrderQueue() {
        return new Queue(properties.getExpireAndDeleteOrderDelay(), false);
    }


    /**
     * 配置交换器
     * */
    @Bean(name = "expireAndDeleteOrderDelayExchange")
    CustomExchange expireAndDeleteOrderDelayExchange() {
        Map<String, Object> args = new HashMap<>();
        args.put("x-delayed-type", "direct");
        return new CustomExchange("expireAndDeleteOrderDelayExchange", "x-delayed-message", true, false, args);
    }

    /**
     * 绑定交换器
     * */
    @Bean(name = "expireAndDeleteOrderBinding")
    Binding expireAndDeleteOrderBinding(@Qualifier("expireAndDeleteOrderDelayQueue") Queue queue, @Qualifier("expireAndDeleteOrderDelayExchange") CustomExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(properties.getExpireAndDeleteOrderDelay()).noargs();
    }

}
